package com.activity;

import util.ApplicationConstant;

import com.activity.R;
import com.fragment.MyLocateFragment;

import dao.FileMyLocate;
import entities.MyLocate;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class AddMyLocate extends Activity {
	private TextView txtAddress;
	private TextView txtName;
	private Button btnOk;
	private FileMyLocate fileMyLocate;
	private String ERROR_HAS_BEEN_ADD = "";
	private String EMPTY_FIELD = "";
	private int editPos = -1;
	private MyLocate oldLocate;
	public static boolean is_updated=false;//bien co thong bao da edit hoac add de cap nhat lai list mylocate
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_my_locate);
		ERROR_HAS_BEEN_ADD = getResources().getString(
				R.string.error_has_been_added);
		EMPTY_FIELD = getResources().getString(R.string.error_empty_field);
		fileMyLocate = new FileMyLocate(getApplicationContext());
		txtAddress = (TextView) findViewById(R.id.txt_add_ml_address);
		if (MainAppActivity.nowAddress != null)
			txtAddress.setText(MainAppActivity.nowAddress);
		txtName = (TextView) findViewById(R.id.txt_add_ml_name);
		btnOk = (Button) findViewById(R.id.btn_add_ml_ok);
		// edit mode
		Intent intent = getIntent();
		if (intent.getExtras() != null) {
			editPos = intent.getExtras().getInt(ApplicationConstant.POSTION, -1);
			if (editPos != -1) {
				oldLocate = MyLocateFragment.listMyLocates.get(editPos);
				txtAddress.setText(oldLocate.getAddress());
				txtName.setText(oldLocate.getName());
			}
		}

		btnOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String name = txtName.getText().toString();
				String address = txtAddress.getText().toString();
				if (name != null && address != null
						&& MainAppActivity.orginalLatl != null) {
					if (name.equals("") || address.equals("")) {
						Toast.makeText(getApplicationContext(), EMPTY_FIELD,
								Toast.LENGTH_SHORT).show();
					} else {
						MyLocate myLocate = new MyLocate();
						myLocate.setAddress(address);
						myLocate.setName(name);
						myLocate.setLa(MainAppActivity.orginalLatl.latitude);
						myLocate.setLog(MainAppActivity.orginalLatl.longitude);
						// add mode or edit mode
						if (editPos == -1) {
							if (fileMyLocate.add(myLocate)) {
								MyLocateFragment.notFoundLayout
										.setVisibility(View.GONE);
								is_updated=true;
								finish();
							} else
								Toast.makeText(getApplicationContext(),
										ERROR_HAS_BEEN_ADD, Toast.LENGTH_SHORT)
										.show();
						} else {
							MyLocate newLocate = new MyLocate();
							newLocate.setAddress(oldLocate.getAddress());
							newLocate.setLa(oldLocate.getLa());
							newLocate.setLog(oldLocate.getLog());
							newLocate.setName(txtName.getText().toString());
							if (fileMyLocate.replace(oldLocate, newLocate))
							{
								is_updated=true;
								finish();
							}
						}
					}
				}

			}
		});

	}

}
